﻿using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using Evolution.Application.SharedKernel.Actions.Bootstrappers;
using Evolution.Client.ImportData.Infrastructure;
using Evolution.Client.ImportData.Mapper;

namespace Evolution.Client.ImportData.Actions
{
    public class DeserializeAndImportDataProcess : SendCommandToBus
    {
        /// <summary>
        /// Deserialize Data Object and send command
        /// </summary>
        public void DoWork()
        {
            Console.WriteLine("\nBEGIN: Deserialize and send data object command");
            var dataObjectsMapping = ImportDataMapper.GetDataObjectsMapping();

            //Deserialzie each data object
            foreach (var objectMapping in dataObjectsMapping)
            {
                Console.WriteLine("\nDeserializing Data Object:[{0}]", objectMapping.Name);
                Console.WriteLine("ClassType: {0}, FilePath: {1}", objectMapping.ClassType, objectMapping.FilePath);

                var dataInstance = Activator.CreateInstance(Type.GetType(objectMapping.ClassType));
                var listObjects = DeserializeDataObject(dataInstance.GetType(), objectMapping.FilePath);
                foreach (var dataObject in listObjects)
                {
                    Console.WriteLine("---");
                    Console.WriteLine(@"Sending command object: [{0}].....", dataObject.GetType().Name);
                    try
                    {
                        SendDataObjectCommand(dataObject.GetType(), dataObject);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                    }
                    
                }
            }
            Console.WriteLine("\nEND: Deserialize and send data object command");
        }

        /// <summary>
        /// Deserialize list of data objects from XML file
        /// </summary>
        /// <param name="objectType"></param>
        /// <param name="fileMapping"></param>
        /// <returns></returns>
        protected IList DeserializeDataObject(Type objectType, string fileMapping)
        {
            var listObjects = ImportDataMapper.GetList(objectType);

            var filePath = string.Format("{0}\\{1}", ImportSettings.GetInstance().ImportFolder, fileMapping);
            var fs = new FileStream(filePath, FileMode.Open);
            var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            var ser = new DataContractSerializer(listObjects.GetType());

            // Deserialize the data and read it from the instance.
            listObjects = (IList)ser.ReadObject(reader, true);
            reader.Close();
            fs.Close();

            return listObjects;
        }

        protected override ActionsDependencyBootstrapper GetActionsDependencyBootstrapper()
        {
            return new ImportActionsDependencyBootstrapper(DependencyResolver, DependencyResolver);
        }
    }
}
